package com.doge.calcalbum.business.skin

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.doge.calcalbum.databinding.LayoutSkinTipBinding
import com.doge.calcalbum.utils.*

class SkinTipLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    private val binding = LayoutSkinTipBinding.inflate(LayoutInflater.from(context), this)
    private val hasDisplay: Boolean get() = spReadBoolean("sd", false, "sk")
    private var animator: ValueAnimator? = null
    private val msgRestartTask = 101
    private val msgRestart2Task = 102
    private val viewHandler = Handler(Looper.getMainLooper()) {
        if (it.what == msgRestartTask) {
            it.target.removeMessages(msgRestartTask)
            startAnimation1()
        } else if (it.what == msgRestart2Task) {
            it.target.removeMessages(msgRestart2Task)
            startAnimation2()
        }
        true
    }
    private var released = false

    init {
        isVisible = !hasDisplay

        if (!hasDisplay) viewHandler.sendEmptyMessage(msgRestartTask)
    }

    fun setDisplayed() {
        spWrite("sd", true, "sk")
        isVisible = false
        release()
    }

    private fun release() {
        released = true
        viewHandler.removeMessages(msgRestartTask)
        viewHandler.removeMessages(msgRestart2Task)
        animate().cancel()
        animator?.cancel()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        release()
    }

    private fun startAnimation1() {
        animator?.cancel()
        if (released) return
        val anim = ValueAnimator.ofFloat(1f, 0f)
        animator = anim
        anim.addUpdateListener {
            val p = it.animatedValue as Float
            binding.animIcon.alpha = p
            log("startAnimation addUpdateListener 1 p $p")
        }
        anim.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                viewHandler.sendEmptyMessageDelayed(msgRestart2Task, 1000L)
            }
        })
        anim.duration = 400L
        anim.start()
    }

    private fun startAnimation2() {
        animator?.cancel()
        if (released) return
        val anim2 = ValueAnimator.ofFloat(0f, 1f)
        animator = anim2
        anim2.addUpdateListener {
            val p = it.animatedValue as Float
            binding.animIcon.alpha = p
            log("startAnimation addUpdateListener 2 p $p")
        }
        anim2.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                viewHandler.sendEmptyMessageDelayed(msgRestartTask, 400L)
            }
        })
        anim2.duration = 400L
        anim2.start()
    }
}